﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.TestDevices.FunctionGenerator.Interface;
using System.ComponentModel;
using Jbe.Composite.Extensions;
using System.Reflection;

namespace Jbe.TestSuite.TestDevices.FunctionGenerator.ControlView.Applications
{
    // INFO: This PresentationModel is responsible for data mapping to get it in a wpf data binding frindly form.
    internal class ConfigurationPresentationModel : PresentationModel<IConfigurationView>
    {
        private static readonly PropertyInfo ConfigProperty = TypeManager.GetProperty<ConfigurationPresentationModel>(x => x.Config);
        private static readonly PropertyInfo SineWaveFormProperty = TypeManager.GetProperty<ConfigurationPresentationModel>(x => x.SineWaveForm);
        private static readonly PropertyInfo SquareWaveFormProperty = TypeManager.GetProperty<ConfigurationPresentationModel>(x => x.SquareWaveForm);
        private static readonly PropertyInfo TriangleWaveFormProperty = TypeManager.GetProperty<ConfigurationPresentationModel>(x => x.TriangleWaveForm);
        private static readonly PropertyInfo SawtoothWaveFormProperty = TypeManager.GetProperty<ConfigurationPresentationModel>(x => x.SawtoothWaveForm);


        private IFunctionGeneratorConfig config;


        public ConfigurationPresentationModel(IConfigurationView view)
            : base(view)
        {
        }


        public IFunctionGeneratorConfig Config
        {
            get { return config; }
            set
            {
                if (config != value)
                {
                    config = value;
                    RaisePropertyChanged(ConfigProperty);
                    RaisePropertyChanged(SineWaveFormProperty);
                    RaisePropertyChanged(SquareWaveFormProperty);
                    RaisePropertyChanged(TriangleWaveFormProperty);
                    RaisePropertyChanged(SawtoothWaveFormProperty);
                }
            }
        }

        public bool SineWaveForm
        {
            get { return config.WaveForm == WaveForm.Sine; }
            set
            {
                if (config.WaveForm != WaveForm.Sine)
                {
                    config.WaveForm = WaveForm.Sine;
                    RaiseWaveFormChanged();
                }
            }
        }

        public bool SquareWaveForm
        {
            get { return config.WaveForm == WaveForm.Square; }
            set
            {
                if (config.WaveForm != WaveForm.Square)
                {
                    config.WaveForm = WaveForm.Square;
                    RaiseWaveFormChanged();
                }
            }
        }

        public bool TriangleWaveForm
        {
            get { return config.WaveForm == WaveForm.Triangle; }
            set
            {
                if (config.WaveForm != WaveForm.Triangle)
                {
                    config.WaveForm = WaveForm.Triangle;
                    RaiseWaveFormChanged();
                }
            }
        }

        public bool SawtoothWaveForm
        {
            get { return config.WaveForm == WaveForm.Sawtooth; }
            set
            {
                if (config.WaveForm != WaveForm.Sawtooth)
                {
                    config.WaveForm = WaveForm.Sawtooth;
                    RaiseWaveFormChanged();
                }
            }
        }


        private void RaiseWaveFormChanged()
        {
            RaisePropertyChanged(SineWaveFormProperty);
            RaisePropertyChanged(SquareWaveFormProperty);
            RaisePropertyChanged(TriangleWaveFormProperty);
            RaisePropertyChanged(SawtoothWaveFormProperty);
        }
    }
}
